
import { generateUUID } from "../../../../cubeUtil";
import { StoreFuncs } from "../../ExcelStore";

export interface IInsertRowOptions {
    sheetName: string;
    activeCol: number;
    activeRow: number;
}
export default {
    canUndo: true,
    execute: (context: GC.Spread.Sheets.Workbook, options: IInsertRowOptions, isUndo: boolean) => {
        const { sheetName, activeRow } = options;
        const sheet = context.getSheetFromName(sheetName);
        const needSuspernded = !sheet.isPaintSuspended()
        if (needSuspernded) {
            sheet.suspendPaint();
        }
        if (isUndo) {

        } else {
            const store = StoreFuncs.getInstance();
            if (store.table && store.dataSource) {
                const { row: startRow } = store.table.dataRange();
                const datas: any[] = store.dataSource.getValue('datas') as any[];
                const newDatas: any[] = [];

                datas.forEach((data, index) => {
                    if (startRow + index === activeRow) {
                        const uuid = generateUUID();
                        store.insertedRows.push(uuid);
                        newDatas.push({ randomFieldId: uuid });
                    }
                    newDatas.push(data);
                });
                store.dataSource.setValue('datas', newDatas);
                store.bingDatas();
            }
            if (needSuspernded) {
                sheet.resumePaint();
            }
        }
    }
};